<?php

namespace App\Http\Controllers\Apps;

use App\Http\Controllers\AppController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class Order extends AppController
{
    public function payment(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'order_no'  => 'required|integer',
            'voucher'   => 'required'
        ]);

        if ($validator->fails()){

            return  $this->ReturnFailed(1,$validator->errors());
        }

        $order = \App\Models\Order::find($post['order_no']);

        if (! $order || $order->status > 0){

            return $this->ReturnVerifyError(24);
        }

        $order->voucher = $post['voucher'];
        $order->status = 1;
        $order->save();

        return $this->ReturnSuccessInfo();
    }

    public function affirm(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'order_no'  => 'required|integer',
            'type'      => 'required|boolean',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        $order = \App\Models\Order::find($post['order_no']);

        if ($order->status != 1){

            return $this->ReturnVerifyError(24);
        }

        $order->status = $post['type'] ? 2 : -1;
        $order->save();

        return $this->ReturnSuccessInfo();
    }
}
